// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        mcNode:{
            displayName:"动画Node",
            type:cc.Node,
            default:null,
        },
        sudu:{ 
            displayName:"速度",
            default:10,
            tooltip:'移动的速度，值越大跑的越快'
       },
       
       tianliMax:{ 
            displayName:"弹跳力",
            default:15,
            tooltip:'跳起的力量，如果重量不变，值越大跑的越高'
       },
       zhongliMax:{ 
            displayName:"重力",
            default:5,
            tooltip:'自身重量，跳起的力量不变，值越大跳的越低，落下的越快，格斗游戏一般5以上'
       },
    },

    // LIFE-CYCLE CALLBACKS:
     hitRect(){
       let r=new cc.Rect(this.node.x-16,-640+this.node.y+55,32,55);
       let pw=32;
       let ph=32;
       let nodeX = r.x;
       let nodeY = r.y;
       let tx =(Math.floor(nodeX/pw));
       let ty =(Math.ceil(nodeY/ph));
       let iw =Math.floor(((r.width+ nodeX-1)/pw));
       let ih = Math.ceil((nodeY-r.height+1)/ph);
       for(let xt=tx;xt<=iw;xt++){
        for(let yt=ty;yt>=ih;yt--){
            let xn=(xt*pw).toString();
            let yn=(yt*ph).toString();
            if(window.hitObj!=null&&window.hitObj[xn]!=null&&window.hitObj[xn][yn]!=null){
                this.node.y=window.hitObj[xn][yn][0].y+640;
            }
        }
       }
     },
     onLoad () {
        this.pzbo=true;
        this.keyArr=[];
        cc.eventManager.addListener({event:cc.EventListener.KEYBOARD,
			onKeyPressed:this.onKeyPressed.bind(this),
            onKeyReleased:this.onKeyReleased.bind(this)
        },this.node);
        this.anim = this.mcNode.getComponent(cc.Animation);
     },

    start () {
       // this.hitRect();
       
    },
	onKeyPressed:function(keyCode, event){
        if(window.xueInt<=0){return;}//
        switch(keyCode) {
            case cc.macro.KEY.a:
            case cc.macro.KEY.left:
                 let sy=this.keyArr.indexOf("左");
                 if(sy==-1){
                    this.keyArr.unshift("左")
                 }
                this.run();
                break;
            case cc.macro.KEY.d:
            case cc.macro.KEY.right:
                let sy2=this.keyArr.indexOf("右");
                if(sy2==-1){
                    this.keyArr.unshift("右")
                }
                this.run();
                break;
            case cc.macro.KEY.k:
                //this.keyTiao();
                this.jump();
                break;
           
            
        }


    },
    run:function(){
        if(this.ssInt>0||window.xueInt<=0){return;}
        if(this.mcID=='待机'||this.mcID=='伤'){
            this.mcID='跑';
            this.anim.play('run');
        }
    },
    jump:function(i){
       // if(window.xueInt<=0){return;}
        if(i!=null){
            this.tl=i;
        }
        if(this.mcID!='跳'){
            this.mcID='跳';
            this.anim.play('jump');
        }
    },
    stand:function(){
        if(this.ssInt>0||window.xueInt<=0){return;}
        if(this.mcID!='待机'&&this.pzbo){
            this.mcID='待机';
            this.anim.play('stand');
        }
    },
    onKeyReleased: function (keyCode, event) {
        switch(keyCode) {
            case cc.macro.KEY.a:
            case cc.macro.KEY.left:
              let sy=this.keyArr.indexOf("左");
              if(sy!=-1){
                  this.keyArr.splice(sy,1);
              }
              break;
            case cc.macro.KEY.d:
            case cc.macro.KEY.right:
                let sy2=this.keyArr.indexOf("右");
                if(sy2!=-1){
                   this.keyArr.splice(sy2,1);
                }
                break;
        }


	},
     update (dt) {
        if(this.keyArr.length==0){
            this.stand();

        }else if(this.keyArr[0]==="左")
		{
            this.mcNode.scaleX=1;
            this.node.x+=this.sudu*this.mcNode.scaleX*-1;
        //    this.updateMapLocation();//
       
		}else if(this.keyArr[0]==="右")
		{
            this.mcNode.scaleX=-1;
            this.node.x+=this.sudu*this.mcNode.scaleX*-1;
         //   this.updateMapLocation();
        }
       // this._private_onWorld();
     
     },
     
     _private_onWorld:function()
    {
        if(window.xueInt<=0||this.mapjs==null){return;}//
        if(!this.pzbo)
        {
            if(this.ptArr.length==0)
            {
                this._public_setY(this.node.y-((this.zhongliMax-this.tl)));
            }else if(this.tl<this.zhongliMax){
                this._public_setY(this.node.y-((this.zhongliMax-this.tl)));
            }
        }else
        {
            return;
        }
        if(this.ptArr.length==0){
            this.tl-=this.zhongliMax/5;
        }else{
            this.tl-=this.zhongliMax/3;
        }
     
       if(this.tl>0){
			this.pzbo=false;
				//	return;
		}


      
      
    },
});
